unit nomenclaturecreateedit;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Buttons,
  StdCtrls, DbCtrls;

type

  { TfrmNomenclatureCreateEdit }

  TfrmNomenclatureCreateEdit = class(TForm)
    btnSave: TBitBtn;
    btnCancel: TBitBtn;
    btnGroupSelect: TBitBtn;
    DBEdtTitle: TDBEdit;
    DBEdtQuantityMin: TDBEdit;
    DBEdtArticle: TDBEdit;
    DBEdtArticleInside: TDBEdit;
    DBEdit5: TDBEdit;
    DBEditPrice: TDBEdit;
    DBEdtPriceSmallWholeSale: TDBEdit;
    DBEdtPriceBigWholeSale: TDBEdit;
    DBEdtPriceRetail: TDBEdit;
    edtGroup: TEdit;
    Label1: TLabel;
    Label10: TLabel;
    lblminquantity: TLabel;
    lblGroup: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    lblallquantity: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    procedure btnSaveClick(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure btnGroupSelectClick(Sender: TObject);
    procedure DBEditPriceKeyPress(Sender: TObject; var Key: char);
    procedure DBEdtQuantityMinChange(Sender: TObject);
    procedure DBEdtPriceSmallWholeSaleChange(Sender: TObject);
    procedure DBEdtPriceBigWholeSaleChange(Sender: TObject);
    procedure DBEdtPriceRetailChange(Sender: TObject);
    procedure DBEditPriceChange(Sender: TObject);
    procedure DBEdtTitleChange(Sender: TObject);
    procedure edtGroupChange(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
    procedure checkErrors;
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  frmNomenclatureCreateEdit: TfrmNomenclatureCreateEdit;

implementation

{$R *.lfm}
uses main,nomenclature,groupselect;

{ TfrmNomenclatureCreateEdit }

procedure TfrmNomenclatureCreateEdit.checkErrors;
begin
  if ((DBEdtTitle.Text=EmptyStr) OR (DBEditPrice.Text=EmptyStr) OR (DBEdtPriceRetail.Text=EmptyStr) OR (DBEdtPriceSmallWholeSale.Text=EmptyStr) OR (DBEdtPriceBigWholeSale.Text=EmptyStr) OR (DBEdtQuantityMin.Text=EmptyStr)) then begin
    btnSave.Enabled:=False;
  end else begin
    btnSave.Enabled:=True;
  end;
end;

procedure TfrmNomenclatureCreateEdit.btnSaveClick(Sender: TObject);
begin
  FrmMain.DSNomenclature.DataSet.Edit;
  if (edtGroup.Text='') then begin
     frmNomenclature.CurrentGroupRecordNumber:=0;
  end;
  FrmMain.DSNomenclature.DataSet.FieldByName('parentid').AsInteger:=frmNomenclature.CurrentGroupRecordNumber;
  FrmMain.DSNomenclature.DataSet.FieldByName('group').AsBoolean:=False;
  FrmMain.DSNomenclature.DataSet.Post;
  FrmMain.DSNomenclature.DataSet.Refresh;
  Close;
end;

procedure TfrmNomenclatureCreateEdit.btnCancelClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmNomenclatureCreateEdit.btnGroupSelectClick(Sender: TObject);
begin
  frmGroupSelect.groupsId:=1;
  frmGroupSelect.ShowModal;
  if frmGroupSelect.selectedGroupId<>EmptyStr then begin
    frmNomenclature.CurrentGroupRecordNumber:=StrToInt(frmGroupSelect.selectedGroupId);
    edtGroup.Text:=frmGroupSelect.selectedGroupTitle;
  end;
end;

procedure TfrmNomenclatureCreateEdit.DBEditPriceKeyPress(Sender: TObject;
  var Key: char);
begin
  FrmMain.KeyVerifyFloat(Sender,Key);
end;

procedure TfrmNomenclatureCreateEdit.DBEdtQuantityMinChange(Sender: TObject);
begin
  checkErrors;
end;

procedure TfrmNomenclatureCreateEdit.DBEdtPriceSmallWholeSaleChange(Sender: TObject);
begin
  checkErrors;
end;

procedure TfrmNomenclatureCreateEdit.DBEdtPriceBigWholeSaleChange(Sender: TObject);
begin
  checkErrors;
end;

procedure TfrmNomenclatureCreateEdit.DBEdtPriceRetailChange(Sender: TObject);
begin
  checkErrors;
end;

procedure TfrmNomenclatureCreateEdit.DBEditPriceChange(Sender: TObject);
begin
  checkErrors;
end;

procedure TfrmNomenclatureCreateEdit.DBEdtTitleChange(Sender: TObject);
begin
  checkErrors;
end;

procedure TfrmNomenclatureCreateEdit.edtGroupChange(Sender: TObject);
begin
  checkErrors;
end;

procedure TfrmNomenclatureCreateEdit.FormCloseQuery(Sender: TObject;
  var CanClose: boolean);
begin
  FrmMain.DSNomenclature.DataSet.Cancel;
  FrmMain.DSNomenclature.DataSet.Refresh;
end;

end.

